// import OpenAI from "openai";
const OpenAI  = require('openai')
const soilDao = require('../database/solidDao')
const pestdiseaseDao = require('../database/pestdiseaseDao')
const riskDao = require('../database/riskDao')
const deviceMoudle = JSON.stringify({
  device:'xxxx',
  risk_level:'xx'
})

const openai = new OpenAI(
    {
        // 若没有配置环境变量，请用百炼API Key将下行替换为：apiKey: "sk-xxx",
        apiKey: "sk-975cabb58b8740cdb71bf3f653a8d3e7",
        baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
    }
);
async function getDevice(farmland_id){
  const soilData = await soilDao.getSolidData(farmland_id)
  const pestdiseaseData = await pestdiseaseDao.getPestData(farmland_id)
  const completion = await openai.chat.completions.create({
    model: "deepseek-r1",  // 此处以 deepseek-r1 为例，可按需更换模型名称。
    messages: [
        { role: "user",
          content: "你好，请根据下面给出的农田的土壤墒情与病虫害情况，给出不超过十二个字的建议，并且评定一下风险等级，风险等级分为‘高’，‘中’，‘低’三级，返回的答案结果请以一个json格式的字符串返回，例如:"+ deviceMoudle + soilData[0] + pestdiseaseData[0] + "不要返回```json```"
        }
    ],
  });
  // 获取答案
  console.log("最终答案：")
  console.log(JSON.parse(completion.choices[0].message.content))
  const {device,risk_level} = JSON.parse(completion.choices[0].message.content)
  // 存入到待办事项的的数据库 根据农田的id 来存入风险等级与建议
  const result = await riskDao.setDevice(farmland_id,device,risk_level)
}




module.exports = getDevice